/************************************************************************

								Heroes IV
					  Copyright 2000, The 3DO Company

	------------------------------------------------------------------
  						 stoning_animation.h

	$Header: /heroes4/stoning_animation.h $

	$NoKeywords: $

 ************************************************************************/

// kill the 255+ character identifier warning.  Templates tend to exceed that easily.
#include "precompiled.h"
#pragma warning( disable: 4786 )
#include "stoning_animation.h"

#include "battlefield.h"
#include "combat_creature.h"
#include "combat_sounds.h"
#include "format_string.h"
#include "game_time.h"
#include "music.h"
#include "options.h"
#include "pixel_24.h"
#include "sound.h"
// -----------------------------------------------------------------
// play animation which turns creature gray
// -----------------------------------------------------------------
t_stoning_animation::t_stoning_animation( t_combat_creature& creature, int killed )
                   : t_counted_animation( creature.get_battlefield() ), m_creature( creature ),
				     t_counted_idle_processor( 100 * 100 / get_combat_animation_speed() )
{
	m_killed = killed;
	m_battlefield.add_text( creature, format_string( "-%i", killed ), 
		                           creature.get_message_color(), k_message_icon_stoned );
}

// -----------------------------------------------------------------
// play animation which turns creature gray
// -----------------------------------------------------------------
void t_stoning_animation::on_idle()
{
	int saturation = m_creature.get_saturation();

	if (saturation == 0)
	{
		t_idle_ptr ref = this;

		m_creature.kill( m_killed );
		suspend_idle_processing();
		animation_ended();
		if (m_creature.get_number() > 0)
		{
			m_creature.set_saturation( 256 );
			m_battlefield.invalidate( m_creature );
			m_creature.set_wait_animation();
			m_battlefield.update_state();
			return;
		}
		if (!m_creature.is_summoned() || m_creature.can_use_rebirth())
		{
			m_creature.set_color( 0, 0, 256, 256 );
			m_battlefield.invalidate( m_creature );
			m_creature.animate_death( true, get_action_message() );
			return;
		}
		m_creature.animate_death( false, get_action_message() );
		return;
	}
	saturation -= 16;
	if (saturation <= 0)
	{
		set_delay( 400 * 100 / get_combat_animation_speed());
		saturation = 0;
	}
	m_creature.set_saturation( saturation );
	m_battlefield.invalidate( m_creature );
}

// -----------------------------------------------------------------
// play animation which turns creature blue
// -----------------------------------------------------------------
t_freeze_animation::t_freeze_animation( t_combat_creature& creature )
                  : t_counted_animation( creature.get_battlefield() ),
				    m_creature( creature ), t_counted_idle_processor( 100 * 100 / get_combat_animation_speed() )
{
	get_combat_sound( k_spell_frozen )->play( get_sound_volume() );
}

// -----------------------------------------------------------------
// play animation which turns creature blue
// -----------------------------------------------------------------
void t_freeze_animation::on_idle()
{
	int brightness = m_creature.get_brightness();
	int delta = m_creature.get_hue_delta();
	int saturation = m_creature.get_saturation();
	int const k_saturation_max = 400;
	int const k_brightness_max = 500;
	int const k_steps = 16;
	int const k_saturation_step = k_saturation_max / k_steps;
	int const k_brightness_step = (k_brightness_max - 256) / k_steps;

	if (delta == 256 && saturation == k_saturation_max)
	{
		t_idle_ptr ref = this;

		suspend_idle_processing();
		animation_ended();
		m_creature.set_wait_animation();
		m_battlefield.update_state();
		return;
	}
	if (delta == 0)
	{
		saturation = 0;
		brightness = 256;
	}
	brightness += k_brightness_step;
	if (brightness > k_brightness_max)
		brightness = k_brightness_max;
	saturation += k_saturation_step;
	if (saturation >= k_saturation_max)
	{
		set_delay( 400 * 100 / get_combat_animation_speed() );
		saturation = k_saturation_max;
		brightness = k_brightness_max;
	}
	m_creature.set_color( 430, 256, saturation, brightness );
	m_battlefield.invalidate( m_creature );
}

